<template>
  <view class="pwd-retrieve-container">
    <uni-forms ref="form" :value="user" labelWidth="80px">
      <uni-forms-item name="oldPassword" :label="$lang('旧密码')">
        <uni-easyinput type="password" v-model="user.oldPassword" :placeholder="$lang('请输入旧密码')" />
      </uni-forms-item>
      <uni-forms-item name="newPassword" :label="$lang('新密码')">
        <uni-easyinput type="password" v-model="user.newPassword" :placeholder="$lang('请输入新密码')" />
      </uni-forms-item>
      <uni-forms-item name="confirmPassword" :label="$lang('确认密码')">
        <uni-easyinput type="password" v-model="user.confirmPassword" :placeholder="$lang('请确认新密码')" />
      </uni-forms-item>
      <button type="primary" class="m-[10px] h-[40px] leading-[40px] rounded-[90px] text-white" @click="submit">{{$lang('提交')}}</button>
    </uni-forms>
  </view>
</template>

<script>
  import { updateUserPwd } from "@/api/system/user"

  export default {
    data() {
      return {
        user: {
          oldPassword: undefined,
          newPassword: undefined,
          confirmPassword: undefined
        },
        rules: {
          oldPassword: {
            rules: [{
              required: true,
              errorMessage: this.$lang('旧密码不能为空')
            }]
          },
          newPassword: {
            rules: [{
                required: true,
                errorMessage: this.$lang('新密码不能为空'),
              },
              {
                minLength: 6,
                maxLength: 20,
                errorMessage: this.$lang('长度在 6 到 20 个字符')
              }
            ]
          },
          confirmPassword: {
            rules: [{
                required: true,
                errorMessage: this.$lang('确认密码不能为空')
              }, {
                validateFunction: (rule, value, data) => data.newPassword === value,
                errorMessage: this.$lang('两次输入的密码不一致')
              }
            ]
          }
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      submit() {
        this.$refs.form.validate().then(res => {
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
            this.$modal.msgSuccess(this.$lang("修改成功"))
          })
        })
      }
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .pwd-retrieve-container {
    padding-top: 36rpx;
    padding: 15px;
  }
</style>
